package org.example.postgraduateproject.Servlet;

import com.google.gson.Gson;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.example.postgraduateproject.Dao.UserDao;
import org.example.postgraduateproject.Dao.imp.UserDaoImp;
import org.example.postgraduateproject.entity.User;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author bin
 * @date 2025-05-14
 */
@WebServlet("/reg.do")
public class RegServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");

        String userName = req.getParameter("userName");
        String userEmail = req.getParameter("userEmail");
        String userPhone = req.getParameter("userPhone");
        String userPass = req.getParameter("userPass");

        UserDao userDao = new UserDaoImp();
        User user = new User();
        user.setUserName(userName);
        user.setEmail(userEmail);
        user.setPhone(userPhone);
        user.setPassWord(userPass);

        int n = userDao.regUser(user);

        PrintWriter out = resp.getWriter();
        Gson gson = new Gson();
        String json = gson.toJson(n);

        out.println(json);

        out.flush();
        out.close();
    }
}
